package com.iflytek.uoamp.agent.monitor.internet;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.log4j.Logger;

/**
 * Created by Administrator on 2017/2/21.
 */
public class InterNetSpeed implements Runnable{
    private static volatile InterNetSpeed instance;

    private Map<String , Object> uploadContent = new ConcurrentHashMap<String, Object>();

    private Map<String , Object> downloadContent = new ConcurrentHashMap<String, Object>();

    private Map<String , Object> upload = new ConcurrentHashMap<String, Object>();

    private Map<String , Object> download = new ConcurrentHashMap<String, Object>();

    private InterNetSpeed(){}

    public static InterNetSpeed getInstance() {
        if (instance == null) {
            synchronized (InterNetSpeed.class) {
                if (instance == null) {
                    instance = new InterNetSpeed();
                }
            }
        }
        return instance;
    }

    public static void setInstance(InterNetSpeed instance) {
        InterNetSpeed.instance = instance;
    }

    public Map<String, Object> getUpload() {
        return upload;
    }

    public void setUpload(Map<String, Object> upload) {
        this.upload = upload;
    }

    public Map<String, Object> getDownload() {
        return download;
    }

    public void setDownload(Map<String, Object> download) {
        this.download = download;
    }

    public Map<String, Object> getUploadContent() {
        return uploadContent;
    }

    public void setUploadContent(Map<String, Object> uploadContent) {
        this.uploadContent = uploadContent;
    }

    public Map<String, Object> getDownloadContent() {
        return downloadContent;
    }

    public void setDownloadContent(Map<String, Object> downloadContent) {
        this.downloadContent = downloadContent;
    }

    @Override
    public void run() {
    	Logger.getRootLogger().info("InterNetSpeed Start!");
        while (true){
//            System.out.println("**********************start*************************");
            uploadContent.clear();
            if(!upload.isEmpty()){
                 for(String key :upload.keySet()){
                     double speed;
                     speed = Integer.parseInt(upload.get(key).toString())/5/(double)1024;
                     uploadContent.put(key , speed);
//                     System.out.println("uploadContent:" + key + " " + speed + "kb/s");
                 }
                upload.clear();
            }

            if(!download.isEmpty()){
                downloadContent.clear();
                for(String key :download.keySet()){
                    double speed;
                    speed = Integer.parseInt(download.get(key).toString())/5/1024;
                    downloadContent.put(key , speed);
//                    System.out.println("downloadContent:" + key + " " + speed + "kb/s");
                }
                download.clear();
            }
//            System.out.println("**********************end***************************\n");

            try {
                Thread.currentThread().sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
